package com.zodiac.alioth.common.api;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by well on 2014/7/29.
 */
@Component
public class ApiServiceFactory {

    Map<ApiRequestKey,ApiHandler> appHandler = new HashMap<ApiRequestKey,ApiHandler>();

    public void registerService(ApiRequestKey apiRequestKey, ApiHandler apiHandler){
        if(apiRequestKey == null|| !StringUtils.hasText(apiRequestKey.getApiName()) ||!StringUtils.hasText(apiRequestKey.getApiVersion())){
            throw new RuntimeException("register app service error,appRequestKey error" + apiRequestKey);
        }
        appHandler.put(apiRequestKey, apiHandler);
    }

    public ApiHandler getService(ApiRequestKey apiRequestKey){
        if(apiRequestKey == null|| !StringUtils.hasText(apiRequestKey.getApiName()) ||!StringUtils.hasText(apiRequestKey.getApiVersion())){
            return null;
        }
        return appHandler.get(apiRequestKey);
    }

    public ApiHandler getService(String apiVersion , String apiName){
        if(!StringUtils.hasText(apiVersion) ||!StringUtils.hasText(apiName)){
            return null;
        }
        ApiRequestKey apiRequestKey = new ApiRequestKey();
        apiRequestKey.setApiName(apiName);
        apiRequestKey.setApiVersion(apiVersion);
        return appHandler.get(apiRequestKey);
    }
}
